//
//  GFUserInfoViewController.m
//  GoodFood
//
//  Created by dllo on 15/11/3.
//  Copyright © 2015年 荣伟峰. All rights reserved.
//

#import "GFUserInfoViewController.h"

@interface GFUserInfoViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray *userInfoArr;
@property (nonatomic, strong) UITableView *userInfoTable;
@property (nonatomic, strong) NSMutableArray *tempArr;
@property (nonatomic, strong) NSMutableArray *fansArr;
@property (nonatomic, strong) AppDelegate *app;
@end

@implementation GFUserInfoViewController

-(void)handleData {
//    [[UIApplicatison sharedApplication] delegate]
    self.app = [[UIApplication sharedApplication] delegate];
    NSString *url = [NSString stringWithFormat:@"http://api.meishixing.com/user/user/userbaseinfo/uid=%@&session_id=%@", self.user_id, self.app.session_id];
    NSLog(@"hahahah%@",url);
    [AFNetHandle GETWithURL:url body:nil httpHead:nil responseStyle:JSON success:^(id result) {
        self.userInfoArr = [NSMutableArray array];
        GFUserInfoModel *model = [GFUserInfoModel modWithDic:result[@"result"]];
        [self.userInfoArr addObject:model];
        self.userInfoTable.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        [self.userInfoTable reloadData];
    } fail:^(NSError *error) {
        
    }];
    
}

-(void)handleDataForFollowingCell {
    
    self.app = [[UIApplication sharedApplication] delegate];
    NSString *url = [NSString stringWithFormat:@"http://api.meishixing.com/user/social/following/uid=%@&session_id=%@&page=1",self.user_id,self.app.session_id];
    [AFNetHandle GETWithURL:url body:nil httpHead:nil responseStyle:JSON success:^(id result) {
        self.tempArr = result[@"result"];
        [self.userInfoTable reloadData];
    } fail:^(NSError *error) {
        
    }];
    
    
}

-(void)handleDataForFansCell {
    
    
    self.app = [[UIApplication sharedApplication] delegate];
    NSString *url = [NSString stringWithFormat:@"http://api.meishixing.com/user/social/fans/uid=%@&session_id=%@&page=1", self.user_id,self.app.session_id];
    [AFNetHandle GETWithURL:url body:nil httpHead:nil responseStyle:JSON success:^(id result) {
        self.fansArr = result[@"result"];
        [self.userInfoTable reloadData];
    } fail:^(NSError *error) {
        
    }];
    
    
}

-(void)creatUserInfoTable {
    
    self.userInfoTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight - 64) style:UITableViewStylePlain];
    self.userInfoTable.delegate = self;
    self.userInfoTable.dataSource = self;
    [self.view addSubview:self.userInfoTable];
    self.userInfoTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.userInfoTable.tableFooterView = [[UIView alloc] init];
    [self.userInfoTable registerClass:[GFUserInfoTableViewCell class] forCellReuseIdentifier:@"userInfo"];
    [self.userInfoTable registerClass:[GFFootmarkTableViewCell class] forCellReuseIdentifier:@"foodmark"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self creatUserInfoTable];
    [self handleData];
    [self handleDataForFollowingCell];
    [self handleDataForFansCell];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    GFUserInfoModel *model = [self.userInfoArr lastObject];
    if (model.last_picture_list.count == 0) {
        return 1;
    } else {
      return 2;
    }
    
    
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (1 == section) {
        return @"Ta最近的美食足迹";
        
    } else {
        return nil;
    }
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    GFUserInfoModel *model = [self.userInfoArr lastObject];
    if (0 == section) {
        return 1;
    } else {
        
        return model.last_picture_list.count + 1;
    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    GFUserInfoModel *model = [self.userInfoArr lastObject];
    if (0 == indexPath.section) {
        return ScreenWidth / 10 + 20 + ScreenWidth / 5;
    } else {
        if (model.last_picture_list.count == indexPath.row) {
            return 44;
        } else {
            return ScreenWidth / 5 + 10;
        }
        
    }
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GFUserInfoModel *model = [self.userInfoArr lastObject];
    if (0 == indexPath.section) {
        
        GFUserInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"userInfo"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.user_id = self.user_id;
        cell.model = model;
        cell.tempArr = self.tempArr;
        cell.fansArr = self.fansArr;
        return cell;
    } else {
        
        if (model.last_picture_list.count == indexPath.row) {
            static NSString *str = @"cell";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
            }
            cell.textLabel.text = @"查看更多足迹";
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
            
        } else {
            NSDictionary *dic = [model.last_picture_list objectAtIndex:indexPath.row];
            GFFootmarkTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"foodmark"];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.tempDic = dic;
            return cell;
        }
        
        
    }
    
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    GFUserInfoModel *model = [self.userInfoArr lastObject];
    if (1 == indexPath.section) {
        if (model.last_picture_list.count == indexPath.row) {
            GFFoodmarkViewController *foodmarkVC = [[GFFoodmarkViewController alloc] init];
            foodmarkVC.user_id = self.user_id;
            [self.navigationController pushViewController:foodmarkVC animated:YES];
        } else {
            
            GFNearHomeDetialViewController *detialVC = [[GFNearHomeDetialViewController alloc] init];
            NSArray *tempArr = model.last_picture_list;
            NSDictionary *dic = [tempArr objectAtIndex:indexPath.row];
            detialVC.picture_id = dic[@"picture_id"];
            detialVC.i = 200;
            [self.navigationController pushViewController:detialVC animated:YES];
        }
    }
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
